package com.sunday.fangdaifu.activity.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.reflect.TypeToken;
import com.sunday.common.base.BaseFragment;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.adapter.DateListAdapter;
import com.sunday.fangdaifu.common.Api;
import com.sunday.fangdaifu.common.ApiClient;
import com.sunday.fangdaifu.common.ApiServiceImpl;
import com.sunday.fangdaifu.model.DateBanner;
import com.sunday.fangdaifu.model.DateContent;
import com.sunday.fangdaifu.model.DateType;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/6/29.
 */

public class DateChildFragment extends BaseFragment implements ApiServiceImpl.HttpResponseInterface {

//    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    private int id;
    private int pageNo = 1;
    private int pageSize = 10;
    private List<DateBanner> dateBannerList = new ArrayList<>();
    private List<DateContent> dateContentList = new ArrayList<>();
    private List<DateType> type = new ArrayList<>();
    private DateListAdapter adapter;
    private LinearLayoutManager manager;


    public static DateChildFragment newInstance(int id) {
        DateChildFragment fragment = new DateChildFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_date_child, container, false);
//        ButterKnife.bind(this, rootView);
        recyclerView= (RecyclerView) rootView.findViewById(R.id.recycler_view);

        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getInt("id");
        }

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        manager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(manager);
        adapter = new DateListAdapter(mContext);
        adapter.setData(type);
        recyclerView.setAdapter(adapter);
        initView();
    }

    private void initView() {
        ApiClient.getApiService().dateBanner(id, this, new TypeToken<ResultDO<List<DateBanner>>>() {
        }.getType());
    }

    private void initListView() {
        ApiClient.getApiService().dateList(id, pageNo, pageSize, this, new TypeToken<ResultDO<List<DateContent>>>() {
        }.getType());
    }

    @Override
    public void onResponce(String key, Object result) {
        if (isFinish) {
            return;
        }
        switch (key) {
            case Api.DATE_LIST:
                ResultDO<List<DateContent>> resultDO = (ResultDO<List<DateContent>>) result;
                if (pageNo == 1)
                    dateContentList.clear();
                dateContentList.addAll(resultDO.getResult());
                DateType dateType1 = new DateType();
                dateType1.setType(1);
                dateType1.setDateContentList(dateContentList);
                type.add(dateType1);
                adapter.setData(type);
                adapter.notifyDataSetChanged();
                break;
            case Api.DATE_BANNER:
                initListView();
                ResultDO<List<DateBanner>> resultDO1 = (ResultDO<List<DateBanner>>) result;
                dateBannerList.addAll(resultDO1.getResult());
                DateType dateType = new DateType();
                dateType.setType(2);
                dateType.setDateBannerList(dateBannerList);
                type.add(dateType);
                adapter.setData(type);
                adapter.notifyDataSetChanged();
                break;
        }

    }

    @Override
    public void onFailure() {
        ToastUtils.showToast(mContext, "服务器异常");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
